﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace auxiliary_proj
{
    public partial class FormCreateUnit : Form
    {
        string unitName, unitEmail, unitPhone, unitPassword, unitAccountBalance;
        private static MySqlConnection connection;  //server connection stuff
        Parser p;

        bool isUnit = false, isEmail = false, isPass = false, isBal = false;

        public FormCreateUnit()
        {
            InitializeComponent(); //initializes the component
            UnitNameTextBox.Focus(); //set the focus to the unit name textbox
            p = new Parser();  //creates a instance of the Parser class
        }

        private void CreateUnit_Load(object sender, EventArgs e)
        {
            Program.parseServerInfo(); //parse the server file
            connection = new MySqlConnection(Program.getConnection());  //establish a connection to the server.
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Dispose(); //dispose of the form
        }

        private void UnitCreateBtn_Click(object sender, EventArgs e)
        {
            unitName = UnitNameTextBox.Text;
            unitEmail = UnitEmailTextBox.Text;
            unitPhone = tbPhone.Text;
            unitPassword = tbUnitPassword.Text;
            unitAccountBalance = tbUnitAcctBalance.Text;

            if ((unitName != null) && (unitEmail != null) && (unitPhone != null) && (unitPassword != null))
            {
                if (isUnit)
                {
                    if (isEmail)
                    {
                        if (isPass)
                        {
                            if (p.testAccountBalRegex(unitAccountBalance))
                            {
                                Program.parseServerInfo(); //parse the server file

                                //insert query
                                string insert = "INSERT INTO `UNIT`(`name`, `email`, `phone_number`, `password`, `Balance`) VALUES ('" + unitName + "','" + unitEmail + "', '" + unitPhone + "', '" + unitPassword + "', '" + unitAccountBalance + "')";
                                if (Query.nonQuery(insert))
                                {
                                    MessageBox.Show("Unit Created!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                    MessageBox.Show("Failed To Create!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                this.Close();
                            }
                            else MessageBox.Show("Invalid Account Balance!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else MessageBox.Show("Invalid Password\n- Must be at least 8 characters, and to be a mix of letters and numbers.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else MessageBox.Show("Invalid Email!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else MessageBox.Show("Invalid Unit Name", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else MessageBox.Show("Please Complete the Form!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void name_textChanged(object sender, EventArgs e)
        {
            isUnit = Parser.isMatch(sender, @"^[a-zA-Z0-9][\sa-zA-Z0-9-]{0,29}$");
        }

        private void email_textChanged(object sender, EventArgs e)
        {
            isEmail = Parser.isMatch(sender, @"\w+@\w+\.+\w{2,}");
        }

        private void pass_textChanged(object sender, EventArgs e)
        {
            isPass = Parser.isMatch(sender, @"^[a-zA-Z][\w!@#$%^&*\(\)-+';:<>`~]{3,15}$");
        }

        private void balance_changeText(object sender, EventArgs e)
        {
            isBal = Parser.isMatch(sender, @"^\d+(\.\d{2}){0,1}$");
        }
    }
}
